using System.Web.Security;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Filters;
using StateTheaterDataModel;
using System.Linq;
using System.Data.Linq;
using System.IO;
using System.Collections.Generic;
using System;
using StateTheater.Helpers;
using System.Xml.Serialization;

namespace StateTheater.Controllers
{
	[Layout("default"), Rescue("generalerror")]
	[Filter(ExecuteWhen.BeforeAction, typeof(MobileFilter))]
	public class NJACTController : PlainTextController
	{
		StateTheaterDataContext db;
		public NJACTController(StateTheaterDataContext db)
		{
			this.db = db;
		}

		public void About()
		{
			var q = from T in db.Troupes
					where T.RECT
					orderby T.Name
					select new
					{
						Name = Util.AdjTitle(T.Name),
						ShortName = T.shortname
					};
			PropertyBag["troupes"] = q.ToList();

			string perryWebFolder = Context.ApplicationPath + "/Views/Perry";
			string perryPCFolder = HttpContext.Server.MapPath(perryWebFolder);

			var years = (from pathName in Directory.GetFiles(perryPCFolder, "perry*.vm")
						 orderby pathName descending
						 select Path.GetFileNameWithoutExtension(pathName).Substring(5)).ToList();

			PropertyBag["years"] = years;
		}


		public void CurrentShows()
		{
			var shows = from p in db.Productions
					   where p.Troupe.Associations.Select(a => a.Organization).Contains("NJACT")
					   && p.FirstPerformance < DateTime.Now && p.LastPerformance > DateTime.Now
					   select new { p.Play.Title, ID= p.ProductionID, Troupe=p.Troupe.Name, p.Venue.City };
			PropertyBag["Shows"] = shows.ToList();
			this.CancelLayout();
			this.Response.ContentType = "text/xml";
		}

		private static readonly int[] musicalCredits = { 20, 25, 27, 30, 35, 40 };


		[XmlType("Details")]
		public class xShowDetails
		{
			public class xShowInfo
			{
				public string Title { get; set; }
				public string Troupe { get; set; }
				public string Venue { get; set; }
				public bool Musical { get; set; }
				public bool Original { get; set; }
			}
			[XmlType("Roles")]
			public class xRoles
			{
				public string Name { get; set; }
				public string Role { get; set; }
			}

			public xShowInfo Info { get; set; }
			public xRoles[] Cast { get; set; }
		}

		[return: XmlReturnBinder ]
		public xShowDetails ShowDetails(int prodId)
		{
			var showInfo = (from pr in db.Productions
					where pr.ProductionID == prodId
					let Musical = pr.Play.PlayCredits.Any(pc => musicalCredits.Contains(pc.CreditID.Value))
					let prev = db.Productions.Any(pr2 => pr2.PlayID == pr.PlayID && pr2.FirstPerformance < pr.FirstPerformance)
					let writtenActor = db.Roles.Any(r => pr.Play.PlayCredits.Select(pc => pc.PersonID).Contains(r.PersonID))
							select new xShowDetails.xShowInfo 
							{ 
								Title = pr.Play.Title, 
								Troupe = pr.Troupe.Name, 
								Venue = pr.Venue.Name,
								Musical = Musical, 
								Original = !prev && writtenActor 
							}).FirstOrDefault();

			if (showInfo == null)
				return new xShowDetails();

			var cast = (from c in db.Roles
						where c.ProductionID == prodId
						select new xShowDetails.xRoles { Name = c.People.FullName, Role = c.Role1 }).ToArray();

			return new xShowDetails { Info = showInfo, Cast = cast };

		}
	}
}
